package fun.hijklmn.magic.admin.server.handler;

import fun.hijklmn.magic.common.constants.CharsetConstant;
import fun.hijklmn.magic.common.constants.ContentTypeConstant;
import fun.hijklmn.magic.common.enums.ResponseCodeEnum;
import fun.hijklmn.magic.common.utils.JsonUtil;
import fun.hijklmn.magic.common.utils.ResultVOUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description
 * @Author fs
 * @Date 2021/7/1 13:28
 * @Version 0.0.1-SNAPSHOT
 **/
@Component
public class JwtAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setCharacterEncoding(CharsetConstant.UTF8);
        httpServletResponse.setContentType(ContentTypeConstant.APPLICATION_JSON);
        httpServletResponse.getWriter().println(JsonUtil.toJsonString(ResultVOUtil.parse(ResponseCodeEnum.PERMISSION_DENIED)));
        httpServletResponse.getWriter().flush();
    }

}
